﻿using LView.CMS.IServices;
using LView.CMS.NLog;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using NLog;
using System;

namespace LView.CMS.Services
{
    public class NLogHelperService : INlogHelperService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;

        private readonly Logger _logger = LogManager.GetCurrentClassLogger();

        public NLogHelperService(IHttpContextAccessor httpContextAccessor)
        {
            _httpContextAccessor = httpContextAccessor;
        }
        public void LogError(Exception ex)
        {
            LogMessage logMessage = new LogMessage();
            logMessage.IpAddress = _httpContextAccessor.HttpContext.Request.Host.Host;
            if (ex.InnerException != null)
                logMessage.LogInfo = ex.InnerException.Message;
            else
                logMessage.LogInfo = ex.Message;
            logMessage.StackTrace = ex.StackTrace;
            logMessage.OperationTime = DateTime.Now;
            logMessage.OperationName = "admin";
            _logger.Error(LogFormat.ErrorFormat(logMessage));
        }

        public void LogInfo(string message)
        {
            LogMessage logMessage = new LogMessage();
            logMessage.IpAddress = _httpContextAccessor.HttpContext.Request.Host.Host;
            logMessage.LogInfo = message;
            logMessage.OperationTime = DateTime.Now;
            logMessage.OperationName = "admin";
            _logger.Info(LogFormat.InfoFormat(logMessage));
        }
    }
}
